﻿using GameBox.Framework;
using GameBox.Service.ByteStorage;
using GameBox.Service.GiantLightServer;
using rpc;
using Tanks.Model;
using UnityEngine;
using WOA.Model;

namespace Rpc
{
    public class Controller : MonoBehaviour
    {
        private static int frame_id = 0;
        // Use this for initialization
        public void Start()
        {
            GlobalLogger.L("Rpc start. Init service...");
            new ServiceTask("com.giant.service.giantlightserver").Start().Continue(task =>
            {
                GlobalLogger.L("Got com.giant.service.giantlightserver.");
                m_Server = ServiceCenter.GetService<IGiantLightServer>();
                m_Server.Connect("127.0.0.1", 8002, m_Client);
                // this.game = task.Result as IGiantGame;
                // this.game.SetUserData("user", new User { Account = "PC"/*, Offline = true*/ });
                // this.game.GotoScene(new SplashScene());
                return null;
            });
            
        }
    
        // Update is called once per frame
        public void Update()
        {
            // View.View.Render(Time.deltaTime);
        }

        public bool IsReady()
        {
            return null != m_Server;
        }

        public static void SendRequest(string service, string method, byte[] content,
            Client.ResponseHandler responseHandler = null)
        {
            uint rpcId = ++m_RpcId;
            m_Client.SetResponseHandler(rpcId, responseHandler);

            m_Server.SendRequest(rpcId, service, method, content);

        }

        public static void RequestMoveTo(Vector3 position)
        {
            var req = new tanks.PositionMsg();
            req.x = position.x;
            req.y = position.y;
            req.z = position.z;
            req.frame_id = ++frame_id;
            SendRequest("tanks.Tanks", "MoveTo", ByteConverter.ProtoBufToBytes(req));
        }

        public static void RequestRotateTo(float r)
        {
            var req = new tanks.RotationMsg();
            req.r = r;
            SendRequest("tanks.Tanks", "RotateTo",ByteConverter.ProtoBufToBytes(req));
        }

        public static void RequestFireTo(Vector3 position,float rotation,float launchForce)
        {
            var req = new tanks.FireMsg();
            req.position = new tanks.PositionMsg();
            req.position.x = position.x;
            req.position.y = position.y;
            req.position.z = position.z;
            req.rotation = rotation;
            req.velocity = launchForce;
            req.shell_id = 0;

            SendRequest(Configure.RequestServerName, "Fire",ByteConverter.ProtoBufToBytes(req));
        }

        public static void RequestShellExplodeTo(uint shell_id,Vector3 position)
        {
            var req = new tanks.ShellExplosionMsg();
            req.position = new tanks.PositionMsg();
            req.position.x = position.x;
            req.position.y = position.y;
            req.position.z = position.z;
            req.shell_id = shell_id;

            SendRequest(Configure.RequestServerName, "ShellExplode",ByteConverter.ProtoBufToBytes(req));
        }

        public static void SendResponse(uint id, byte[] content)
        {
            m_Server.SendResponse(id,content);
        }

        private static IGiantLightServer m_Server = null;
        private static Client m_Client = new Client();
        private static uint m_RpcId = 0;  // for SendRequest()
    }  // class Controller
}  // namespace Rpc
